package com.xls.boxlibrary.customcontrol;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;

import com.xls.boxlibrary.R;

import java.lang.reflect.Field;

public class StrokeTextView extends androidx.appcompat.widget.AppCompatTextView {

    TextPaint m_TextPaint;

    int mInnerColor;

    int mOuterColor;

    public StrokeTextView(Context context, int outerColor, int innnerColor) {

        super(context);

        m_TextPaint =this.getPaint();

        this.mInnerColor = innnerColor;

        this.mOuterColor = outerColor;

        // TODO Auto-generated constructor stub

    }

    public StrokeTextView(Context context, AttributeSet attrs) {

        super(context, attrs);

        m_TextPaint =this.getPaint();

        //获取自定义的XML属性名称

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);

        //获取对应的属性值

        this.mInnerColor = a.getColor(R.styleable.StrokeTextView_innnerColor,0xffffff);

        this.mOuterColor = a.getColor(R.styleable.StrokeTextView_outerColor,0xffffff);

    }

    public StrokeTextView(Context context, AttributeSet attrs, int defStyle,int outerColor,int innnerColor) {

        super(context, attrs, defStyle);

        m_TextPaint =this.getPaint();

        this.mInnerColor = innnerColor;

        this.mOuterColor = outerColor;

        // TODO Auto-generated constructor stub

    }

    private boolean m_bDrawSideLine =true; // 默认采用描边

    /**

     *

     */

    @Override

    protected void onDraw(Canvas canvas) {

        if (m_bDrawSideLine) {

// 描外层

            // super.setTextColor(Color.BLUE); // 不能直接这么设，如此会导致递归

//      setTextColorUseReflection(Color.YELLOW);

            setTextColorUseReflection(getResources().getColor(R.color.color_CC7B02));

            m_TextPaint.setStrokeWidth(10); // 描边宽度

            m_TextPaint.setStyle(Paint.Style.FILL_AND_STROKE); // 描边种类

            m_TextPaint.setFakeBoldText(true); // 外层text采用粗体

            //radius：阴影半径  dx：X轴方向的偏移量  dy：Y轴方向的偏移量  color：阴影颜色

            m_TextPaint.setShadowLayer(5, 0,0,0); // 字体的阴影效果，可以忽略

            super.onDraw(canvas);

            // 描内层，恢复原先的画笔

            // super.setTextColor(Color.BLUE); // 不能直接这么设，如此会导致递归

            setTextColorUseReflection(Color.WHITE);

            m_TextPaint.setStrokeWidth(0);

//      m_TextPaint.setStyle(Style.FILL_AND_STROKE);

            m_TextPaint.setFakeBoldText(false);

            m_TextPaint.setShadowLayer(0, 0, 0, 0);

        }

        super.onDraw(canvas);

    }

    /**

     * 使用反射的方法进行字体颜色的设置

     * @param color

     */

    @SuppressLint("SoonBlockedPrivateApi")
    private void setTextColorUseReflection(int color) {

        Field textColorField;

        try {

            textColorField = TextView.class.getDeclaredField("mCurTextColor");

            textColorField.setAccessible(true);

            textColorField.set(this, color);

            textColorField.setAccessible(false);

        }catch (NoSuchFieldException e) {

            e.printStackTrace();

        }catch (IllegalArgumentException e) {

            e.printStackTrace();

        }catch (IllegalAccessException e) {

            e.printStackTrace();

        }

        m_TextPaint.setColor(color);

    }

}
